/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectTeamTypeController
 * 项目组分类
 *
 * 修改纪录
 * 2021-08-09 版本：1.0 mengmeng 创建。
 * @version 版本：1.0
 * @author 作者：mengmeng
 * 创建日期：2021-08-09
 */

package com.topscomm.mpm.controller;

import java.util.List;
import java.util.Map;

import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectTeamEntity;
import com.topscomm.mpm.service.IMpmProjectTeamService;
import com.topscomm.mpm.service.impl.MpmProjectTeamService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mpm.controller.auto.MpmProjectTeamTypeControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

import javax.annotation.Resource;

/**
 * @author: mengmeng
 * @date: 2021-08-09
 * @description:项目组分类Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectTeamType")
public class MpmProjectTeamTypeController extends MpmProjectTeamTypeControllerAuto implements IMpmProjectTeamTypeController {
	/**
	 * @author: mengmeng
	 * @date: 2021-08-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("appointmentId")) {
			String value = paraMap.get("appointmentId").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and appointmentid = {0} ", value);
		}
		if (paraMap.containsKey("projectCode")) {
			String value = paraMap.get("projectCode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectcode = '{0}' ", value);
		}
		// 任命-项目组默认排序,先按照sortcode排序，再按照创建时间排序
		sql.appendSingle(" order by sortcode asc, grouptype desc, createon asc ");
		return sql.toString();
	}

	/**
	 * @author: mengmeng
	 * @date: 2021-08-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	@Override
	protected void afterUpdate(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		StringBufferProxy str = new StringBufferProxy();
		String appointmentid = ConvertUtil.convertToString(paraMap.get("appointmentid"));
		String id = ConvertUtil.convertToString(paraMap.get("id"));
		str.appendSingle("appointmentid = '{0}' and teamid = '{1}'", appointmentid, id);
		MpmProjectTeamService mpmProjectTeamService = (MpmProjectTeamService) SpringUtil
				.getBean("mpmProjectTeamService");
		List<MpmProjectTeamEntity> mpmProjectTeamEntities = mpmProjectTeamService.queryByWhere(str.toString());
		for (MpmProjectTeamEntity mpmProjectTeamEntity : mpmProjectTeamEntities) {
			mpmProjectTeamEntity.setWorkgroup(ConvertUtil.convertToString(paraMap.get("teamname")));
			if (mpmProjectTeamEntity.getGrouptype() > 0 && MpmSystemConst.WorkGroupRoleCode.groupLeader.equals(mpmProjectTeamEntity.getProjectrole())) {
				mpmProjectTeamEntity.setDuty(ConvertUtil.convertToString(paraMap.get("teamname")) + "责任人");
			}
			mpmProjectTeamService.updateEntity(mpmProjectTeamEntity);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-07-13
	 * @return
	 * @description:添加模板工作组
	 * @modify:
	 */
	@PostMapping("/addTemplateGroup")
	public ResponseResult<Object> addTemplateGroup(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> teamTypeMap = this.service.addTemplateGroup(paraMap);
			return ResponseResult.ok(teamTypeMap);
		} catch (Exception var5) {
			return this.handleControllerException(var5);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2023-03-29
	 * @return
	 * @description:撤销工作组/恢复撤销工作组
	 * @modify:
	 */
	@PutMapping("/revokeTeamType")
	public ResponseResult<Object> revokeTeamType(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			if (paraMap.containsKey("type")) {
				if ("revoke".equals(ConvertUtil.convertToString(paraMap.get("type")))) {
					paraMap.put("appointmenttype", MpmSystemConst.MpmTeamTypeAppointment.revoke);
				} else {
					paraMap.put("appointmenttype", MpmSystemConst.MpmTeamTypeAppointment.appointment);
				}
			}
			String str = this.service.revokeTeamType(paraMap);
			return ResponseResult.ok(MpmSystemConst.MpmTeamTypeAppointment.revoke.equals(str) ? "撤销成功！" : "取消撤销成功！");
		} catch (Exception var5) {
			return this.handleControllerException(var5);
		}
	}
}

